package com.atguigu.gulimail.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimail.product.entity.ProductAttrValueEntity;
import com.atguigu.gulimail.product.service.ProductAttrValueService;
import com.atguigu.gulimail.product.vo.AttrResponVo;
import com.atguigu.gulimail.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimail.product.service.AttrService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 商品属性
 *
 * @author xujiale
 * @email 2519970360@qq.com
 * @date 2023-10-24 15:34:13
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService attrValueService;

    /**
     * 修改spu规格
     */
    @PostMapping("update/{spuId}")
    public R updateSpuAttrValue(@RequestBody List<ProductAttrValueEntity> attrValueEntities,@PathVariable Long spuId){
        attrValueService.updateSpuAttrValue(attrValueEntities,spuId);
        return R.ok();
    }

    /**
     * 查出spu的规格属性 : pms_product_attr_value
     */

    @GetMapping("base/listforspu/{spuId}")
    public R spuInfo(@PathVariable Long spuId){

        List<ProductAttrValueEntity> list=attrValueService.baseAttrlistforSpu(spuId);

        return R.ok().put("data",list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType")String type){

        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,type);
        return R.ok().put("page", page);
    }

//    @RequestMapping("/base/list/{cateLogId}")
//    public R baseList(@RequestParam Map<String, Object> params,@PathVariable Long cateLogId){
//
//        PageUtils page = attrService.queryBaseListPage(params,cateLogId);
//
//        return R.ok().put("page", page);
//    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrResponVo attrResponVo = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attrResponVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrResponVo attrResponVo){
		attrService.updateAttr(attrResponVo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
